﻿
using MetroNextTools.NData;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
namespace MetroNextTools.NPanel
{
    public class NWaterWallPanel : NCollectionPanel
    {
        #region Caculate

        private int _columnCount = 2;
        private double _containerHeight = 582;
        private double _containerWidth = 480;
        private Orientation _orientation = Orientation.Horizontal;

        private double[] _columnLengths = null;
        private double _margin = 5;

        public NWaterWallPanel()
        {
            _columnLengths = new double[_columnCount];
        }


        public override Rect CaculatePosition(INRaitoItem ratioItem)
        {
            double width = 0.0;
            double height = 0.0;
            double x = 0.0;
            double y = 0.0;
            if (this._orientation == Orientation.Vertical)
            {
                int lowestIndex = GetLowestIndex();
                width = _containerWidth / _columnCount;
                height = ratioItem.ImgRatio * width;

                x = (_margin + width) * lowestIndex;
                y = _columnLengths[lowestIndex] + _margin;
                if (_columnLengths[lowestIndex] == 0.0)
                {
                    y = 0.0;
                }
                _columnLengths[lowestIndex] = y + height;
            }
            else if (this._orientation == Orientation.Horizontal)
            {
                height = (_containerHeight - _margin) / _columnCount;
                width = height / ratioItem.ImgRatio;
                int lowestIndex = GetLowestIndex();
                y = (_margin + height) * lowestIndex;
                x = _columnLengths[lowestIndex] + _margin;
                if (_columnLengths[lowestIndex] == 0.0)
                {
                    x = 0.0;
                }
                _columnLengths[lowestIndex] = x + width;
            }
            //Debug.WriteLine("x->" + x + "y->" + y + "width:" + width + "height:" + height);
            return new Rect(x, y, width, height);
        }

        public int GetLowestIndex()
        {
            int index = 0;
            for (int i = 1; i < _columnCount; i++)
            {
                if (_columnLengths[index] > _columnLengths[i])
                {
                    index = i;
                }
            }
            return index;
        }


        public override double GetMaxLength()
        {
            int index = 0;
            for (int i = 1; i < _columnCount; i++)
            {
                if (_columnLengths[index] < _columnLengths[i])
                {
                    index = i;
                }
            }
            return _columnLengths[index];
        }

        #endregion


        

    }
}
